package cn.imvc.app.models;

import java.util.Date;
import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;

import cn.imvc.app.domains.AppResult;
import cn.imvc.app.domains.FundFlowType;
import cn.imvc.app.domains.OrderState;
import cn.imvc.app.domains.OrderType;
import cn.imvc.app.entities.Account;
import cn.imvc.app.entities.Bankcard;
import cn.imvc.app.entities.Member;
import cn.imvc.app.entities.PointsToMoney;
import cn.imvc.app.entities.Withdrawals;
import cn.imvc.app.repositories.PointsToMoneyRepository;
import cn.imvc.app.repositories.WithdrawalsRepository;
import cn.imvc.data.domain.ViewRequest;
import cn.imvc.util.RandomId;

@Service
public class WithdrawalsModelImpl implements WithdrawalsModel {

	@Autowired
	private WithdrawalsRepository withdrawalsRepository;
	
	@Autowired
	private PointsToMoneyRepository pointsToMoneyRepository;
	
	@Autowired
	private AccountModel accountModel;
	
	@Autowired
	private FundFlowModel fundFlowModel;

	public Withdrawals get(int id) {
		return withdrawalsRepository.findOne(id);
	}

	public Page<Withdrawals> find(ViewRequest viewRequest) {
		return (Page<Withdrawals>) withdrawalsRepository.findAll();
	}

	public Withdrawals save(Withdrawals withdrawals) {
		Withdrawals withdrawals2 = withdrawalsRepository.saveAndFlush(withdrawals);
		return withdrawals2;
	}

	public Withdrawals update(Withdrawals newWithdrawals) {
		Withdrawals loadWithdrawals = get(newWithdrawals.getId());
		BeanUtils.copyProperties(newWithdrawals, loadWithdrawals, new String[] { "id" });
		return withdrawalsRepository.saveAndFlush(loadWithdrawals);
	}

	public void delete(int id) {
		withdrawalsRepository.delete(id);
	}

	public void deleteBatch(int[] ids) {
		for (int id : ids) {
			delete(id);
		}
	}
	
	public void saveWithdrawalsOrder(Integer money,Member member,Integer bankcardId){
		Withdrawals withdrawals=new Withdrawals();
		Bankcard bankcard=new Bankcard();
		bankcard.setId(bankcardId);
		withdrawals.setMember(member);
		withdrawals.setCreatedAt(new Date());
		withdrawals.setUpdatedAt(new Date());
		withdrawals.setMoney(money);
		withdrawals.setState(OrderState.Create.getValue());
		withdrawals.setCode(OrderType.Withdrawals.getValue()+RandomId.randomId(new Date().getTime()));
		withdrawals.setBankcard(bankcard);
		withdrawalsRepository.save(withdrawals);
		accountModel.retrySaveAccount(member.getAccount().getId(), -money, 0, 0);		
		fundFlowModel.addFundFlow(member, -money, withdrawals.getCode(), FundFlowType.Withdrawals.getValue(),member);
	}

	public List<PointsToMoney> findPointsToMoney(int level) {
		
		return pointsToMoneyRepository.findByEndTimeGreaterThanAndStartTimeLessThanAndLevelOrderByPoints(new Date(), new Date(), level);
	}

	public AppResult savePToMOrder(Integer points, Member member, Integer listId) {
		AppResult result=new AppResult();	
		PointsToMoney pointsToMoney=pointsToMoneyRepository.findOne(listId);
		if (pointsToMoney==null){
			result.setCode("0");
			result.setMsg("没有该兑换方式");
			return result;
		}
		if(pointsToMoney.getPoints()>points){
			result.setCode("0");
			result.setMsg("没有达到该积分兑换方式的最低积分");
			return result;
		}
		Account account=member.getAccount();
		if(account.getPoints()<points){
			result.setCode("0");
			result.setMsg("积分不够");
			return result;
		}
		String code= OrderType.PointsToMoney.getValue() + RandomId.randomId(new Date().getTime());
		Integer money=points*pointsToMoney.getPercent()/100;
		fundFlowModel.addFundFlow(member, -points, code, FundFlowType.PointsToMoney_Points.getValue(), member);
		fundFlowModel.addFundFlow(member, money, code, FundFlowType.PointsToMoney_Money.getValue(), member);
		accountModel.retrySaveAccount(account.getId(), money, 0, -points);
		result.setCode("1");
		result.setMsg("兑换成功");
		return result;
	}
	
	
}
